#include "AlgebraicOperationDialog.h"
#include <QFileDialog>
#include <QMessageBox>
#include "AlgebraicSum.h"
#include "AlgebraicDifference.h"
#include "AlgebraicProduction.h"
#include "AlgebraicQuotient.h"
#include "Ptr.hpp"



AlgebraicOperationDialog::AlgebraicOperationDialog (QWidget *parent)
: QDialog (parent), _pSecondImage (new QImage), _pPreviewImage (new QImage),
_hasImageLoaded (false), _pAlgebraic (NULL)
{
  ui.setupUi(this);
}



AlgebraicOperationDialog::~AlgebraicOperationDialog () {}



void AlgebraicOperationDialog::chooseImage ()
{
  // Show open file dialog
  bool loadSuccess = true;
  QString imgPath = QFileDialog::getOpenFileName (this,
    tr("Open..."), "./", tr("Image Files (*.png *.jpg *.bmp)"));

  if (imgPath.isEmpty ()) // User cancel
    loadSuccess = false;

  if (!_pSecondImage->load (imgPath)) // load fail
  {
    QMessageBox::critical (this, tr ("Open Image Fail"),
      tr ("Cannot open the image: File is corrupted "
      "or in unknown image format."));
    loadSuccess = false;
  }

  ui.rbNormal->setChecked (true);

  if (loadSuccess)
    _hasImageLoaded = true;
    
  ui.lbSecImg->setText (_hasImageLoaded ? imgPath : "No Image");
  ui.rbSum->setEnabled (_hasImageLoaded);
  ui.rbDifference->setEnabled (_hasImageLoaded);
  ui.rbProduction->setEnabled (_hasImageLoaded);
  ui.rbQuotient->setEnabled (_hasImageLoaded);
  _doOperation ();
}



void AlgebraicOperationDialog::initialize (Document * pImgInfo)
{
  _pImgInfo = pImgInfo;
}

void AlgebraicOperationDialog::_doOperation (Ptr< AlgebraicOperation > pAlgebraic)
{
  if (!_hasImageLoaded) return;
  
  if (pAlgebraic != NULL) _pAlgebraic = pAlgebraic;

  (*_pPreviewImage) = _pImgInfo->getPImage ()->copy (_previewRegion);
  QImage secondImage = _pSecondImage->copy (_previewRegion);
  int targetBytesPerPixel = _pPreviewImage->depth () / 8;
  int srcBytesPerPixel = secondImage.depth () / 8;

  for (int v = 0; v < _pPreviewImage->height (); ++v)
  {
    uchar * previewLine = _pPreviewImage->scanLine (v);
    const uchar * secondLine = secondImage.scanLine (v);
    for (int u = 0; u < _pPreviewImage->width (); ++u)
    {
      
    }
  }
  ui.previewCanvas->update ();
}



void AlgebraicOperationDialog::closeEvent (QCloseEvent * e)
{
  _pSecondImage = NULL;
  _pPreviewImage = NULL;
}

void AlgebraicOperationDialog::showEvent (QShowEvent * e)
{
  _pSecondImage = new QImage ();
  _pPreviewImage = new QImage ();
  chooseImage ();
}

void AlgebraicOperationDialog::resizeEvent (QResizeEvent * e)
{
  QImage * pImage = _pImgInfo->getPImagePreview ();
  QRect imageRect  = pImage->rect ();
  QRect canvasRect = ui.previewCanvas->geometry ();
  canvasRect.moveTopLeft (QPoint (0, 0));
  _previewRegion = imageRect & canvasRect;
  _previewRegion.translate (imageRect.center () - _previewRegion.center ());
  _doOperation ();
}




void AlgebraicOperationDialog::setToNormal (bool isChecked)
{
  if (isChecked)
    _doOperation (new AlgebraicOperation);
}

void AlgebraicOperationDialog::setToSum (bool isChecked)
{

  if (isChecked)
    _doOperation (new AlgebraicSum);
}

void AlgebraicOperationDialog::setToDifference (bool isChecked)
{
  if (isChecked)
    _doOperation (new AlgebraicDifference);
}

void AlgebraicOperationDialog::setToProduction (bool isChecked)
{
  if (isChecked)
    _doOperation (new AlgebraicProduction);
}

void AlgebraicOperationDialog::setToQuotient (bool isChecked)
{
  if (isChecked)
    _doOperation (new AlgebraicQuotient);
}
